package com.gzy.fisherybackend.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gzy.fisherybackend.common.BaseResponse;
import com.gzy.fisherybackend.common.ErrorCode;
import com.gzy.fisherybackend.exception.BusinessException;
import com.gzy.fisherybackend.model.dto.FishSpeciesAddRequest;
import com.gzy.fisherybackend.model.dto.FishSpeciesUpdateRequest;
import com.gzy.fisherybackend.model.entity.FishSpecies;
import com.gzy.fisherybackend.service.FishSpeciesService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/fish-species")
public class FishSpeciesController {

    @Resource
    private FishSpeciesService fishSpeciesService;

    /**
     * 获取所有鱼类种类，按创建时间倒序排列，使新添加的数据显示在前面
     */
    @GetMapping
    public BaseResponse<List<FishSpecies>> getAllFishSpecies() {
        // 创建查询条件，按创建时间倒序排序
        QueryWrapper<FishSpecies> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        // queryWrapper.last("ORDER BY create_time DESC, id DESC");
        
        List<FishSpecies> fishSpeciesList = fishSpeciesService.list(queryWrapper);
        return BaseResponse.success(fishSpeciesList);
    }

    /**
     * 获取所有鱼类种类数量
     */
    @GetMapping("/total")
    public BaseResponse<Long> getFishSpeciesCount() {
        QueryWrapper<FishSpecies> queryWrapper = new QueryWrapper<>();
        Long totalCount = fishSpeciesService.count(queryWrapper);
        return BaseResponse.success(totalCount);
    }


    /**
     * 添加新鱼类
     */
    @PostMapping
    public BaseResponse<Long> addFishSpecies(@RequestBody FishSpeciesAddRequest fishSpeciesAddRequest) {
        if (fishSpeciesAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        
        FishSpecies fishSpecies = new FishSpecies();
        BeanUtils.copyProperties(fishSpeciesAddRequest, fishSpecies);
        
        // 设置创建和更新时间
        Date now = new Date();
        fishSpecies.setCreateTime(now);
        fishSpecies.setUpdateTime(now);
        fishSpecies.setIsDelete(0);
        
        boolean result = fishSpeciesService.save(fishSpecies);
        if (!result) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        
        return BaseResponse.success(fishSpecies.getId());
    }

    /**
     * 更新鱼类信息
     */
    @PutMapping("/{id}")
    public BaseResponse<Boolean> updateFishSpecies(@PathVariable Long id, @RequestBody FishSpeciesUpdateRequest fishSpeciesUpdateRequest) {
        if (id <= 0 || fishSpeciesUpdateRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        
        FishSpecies fishSpecies = fishSpeciesService.getById(id);
        if (fishSpecies == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        
        BeanUtils.copyProperties(fishSpeciesUpdateRequest, fishSpecies);
        fishSpecies.setId(id);
        fishSpecies.setUpdateTime(new Date());
        
        boolean result = fishSpeciesService.updateById(fishSpecies);
        if (!result) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        
        return BaseResponse.success(true);
    }

    /**
     * 删除鱼类
     */
    @DeleteMapping("/{id}")
    public BaseResponse<Boolean> deleteFishSpecies(@PathVariable Long id) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        
        boolean result = fishSpeciesService.removeById(id);
        if (!result) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        
        return BaseResponse.success(true);
    }
} 